プロンプトからパイプラインへ
LLMとのインタラクションの進化
以前の授業では、単一のプロンプトによる対話に焦点を当ててきました。しかし、現実世界のアプリケーションでは、一度きりの質問と回答以上のものが必要です。スケーラブルなAIシステムを構築するためには、オーケストレーションに移行しなければなりません。これは複数のLLM呼び出しを連結すること、ユーザー入力に基づいた分岐ロジックの設定、そしてモデルが外部データとやり取りできるようにすることを含みます。
オーケストレーションの基本構成要素
- LLMChain: 基本的な単位です。プロンプトテンプレートと言語モデルを組み合わせたものです。
- シーケンシャルチェーン: これにより、1つのステップの出力が次のステップの入力となる、マルチステップのワークフローを作成できます。
- ルーターチェーン: これらは「トラフィックコントローラー」の役割を果たし、特定のリクエスト(例:数学の質問を「数学チェーン」に、歴史の質問を「歴史チェーン」に)を処理する専門的なサブチェーンを決定するために、LLMを使用します。
核心原則:チェーン則
チェーンは、モデル、プロンプト、メモリといった複数のコンポーネントを一つの整合性のあるアプリケーションに統合できます。このモジュール性により、複雑なタスクを管理可能でデバッグ可能な段階に分解することが保証されます。
プロテイプ:パイプラインのデバッグ
パイプラインが複雑になると、
langchain.debug = True を使用してください。この「レントゲン視覚」により、チェーンの各段階で実際に送信されたプロンプトや受信した原始出力を詳細に確認できます。TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
Question 1
In LangChain, what is the primary difference between a
SimpleSequentialChain and a standard SequentialChain?Challenge: Library Support Router
Design a routing mechanism for a specialized bot.
You are building a support bot for a library.
Define the logic for a
Define the logic for a
RouterChain that distinguishes between "Book Recommendations" and "Operating Hours."
Step 1
Create two prompt templates: one for book suggestions and one for library schedule info.
Solution:
book_template = """You are a librarian. Recommend books based on: {input}"""
schedule_template = """You are a receptionist. Answer hours queries: {input}"""
prompt_infos = [
{"name": "books", "description": "Good for recommending books", "prompt_template": book_template},
{"name": "schedule", "description": "Good for answering operating hours", "prompt_template": schedule_template}
]Step 2
Define the
router_template to guide the LLM on how to classify the user's intent, and initialize the chain.Solution:
router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(
destinations=destinations_str
)
router_prompt = PromptTemplate(
template=router_template,
input_variables=["input"],
output_parser=RouterOutputParser(),
)
router_chain = LLMRouterChain.from_llm(llm, router_prompt)
chain = MultiPromptChain(
router_chain=router_chain,
destination_chains=destination_chains,
default_chain=default_chain,
verbose=True
)